package com.mike.server.engine.socket;

import java.io.IOException;
import java.nio.BufferUnderflowException;
import java.nio.channels.ClosedChannelException;

import org.apache.log4j.Logger;
import org.xsocket.MaxReadSizeExceededException;
import org.xsocket.connection.IConnectExceptionHandler;
import org.xsocket.connection.IConnectHandler;
import org.xsocket.connection.IConnectionTimeoutHandler;
import org.xsocket.connection.IDataHandler;
import org.xsocket.connection.IDisconnectHandler;
import org.xsocket.connection.IIdleTimeoutHandler;
import org.xsocket.connection.INonBlockingConnection;

import com.mike.server.engine.GameMessage;
import com.mike.server.engine.ServiceDispatherThread;
import com.mike.server.engine.socket.support.AbstractEngineSocketHandler;

/**
 * socket 连接事件处理控制
 * @author <a href="zhaoming23@gmail.com"> Mike </a>
 * @version 1.0
 * 2011-4-4 下午08:55:47
 */
public class EngineSocketHandler extends AbstractEngineSocketHandler
implements IConnectHandler, 
IConnectExceptionHandler, IConnectionTimeoutHandler,
IDataHandler, IIdleTimeoutHandler, IDisconnectHandler{

	private static final Logger LOG = Logger.getLogger(EngineSocketHandler.class);
	
	private static EngineSocketHandler instance = new EngineSocketHandler();
	
	private EngineSocketHandler() {
		//Don't allow new it outside.
	}
	
	public static EngineSocketHandler getInstance() {
		return instance;
	}
	
	@Override
	public boolean onConnect(INonBlockingConnection connection)
	throws IOException, BufferUnderflowException,
	MaxReadSizeExceededException {
		LOG.debug("onConnect...");
		return true;
	}
	
	@Override
	public boolean onData(INonBlockingConnection connection) throws IOException,
			BufferUnderflowException, ClosedChannelException,
			MaxReadSizeExceededException {
		LOG.debug("onData...");
		GameMessage message = new GameMessage();
		while(true) {
			buff.clear();
			int read = connection.read(buff.buf());
			if(read <= -1) break;
			buff.flip();
			message.put(buff.array());
		}
		if(message.getType() != -1 && message.getId() != -1) {
			ServiceDispatherThread.getInstance().addMessage(message);
		}
		return true;
	}

	@Override
	public boolean onConnectException(INonBlockingConnection connection,
			IOException ioe) throws IOException {
		//TODO do something when connection exception... and return true
		return false;
	}

	@Override
	public boolean onDisconnect(INonBlockingConnection connection)
			throws IOException {
		//TODO do something when connection disconnect... and return true
		return false;
	}

	@Override
	public boolean onIdleTimeout(INonBlockingConnection arg0)
			throws IOException {
		//TODO do something when connection idle timeout... and return true
		return false;
	}

	@Override
	public boolean onConnectionTimeout(INonBlockingConnection connection)
			throws IOException {
		//TODO do something when connection timeout... and return true
		return false;
	}

}
